﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using FoodWar.Episode1.Components;
using Microsoft.Xna.Framework;

namespace FoodWar.Episode1.Controller
{
    public class XBOXGamePad
    {
        public static int GetNumberOfXBOXGamePads()
        {
            int res = 0;
            if (GamePad.GetState(PlayerIndex.One).IsConnected) res++;
            if (GamePad.GetState(PlayerIndex.Two).IsConnected) res++;
            if (GamePad.GetState(PlayerIndex.Three).IsConnected) res++;
            if (GamePad.GetState(PlayerIndex.Four).IsConnected) res++;
            return res;
        }

        public static bool IsAnyButtonPressed(PlayerIndex index)
        {
            if (GamePad.GetState(index).Buttons.A == ButtonState.Pressed) return true;
            if (GamePad.GetState(index).Buttons.B == ButtonState.Pressed) return true;
            if (GamePad.GetState(index).Buttons.X == ButtonState.Pressed) return true;
            if (GamePad.GetState(index).Buttons.Y == ButtonState.Pressed) return true;
            if (GamePad.GetState(index).Buttons.Start == ButtonState.Pressed) return true;
            if (GamePad.GetState(index).Buttons.RightStick == ButtonState.Pressed) return true;
            if (GamePad.GetState(index).Buttons.RightShoulder == ButtonState.Pressed) return true;
            if (GamePad.GetState(index).Buttons.LeftStick == ButtonState.Pressed) return true;
            if (GamePad.GetState(index).Buttons.LeftShoulder == ButtonState.Pressed) return true;
            return false;
        }

        public static bool IsAnyButtonAtAnyGamePadPressed()
        {
            if (IsAnyButtonPressed(PlayerIndex.One)) return true;
            if (IsAnyButtonPressed(PlayerIndex.Two)) return true;
            if (IsAnyButtonPressed(PlayerIndex.Three)) return true;
            if (IsAnyButtonPressed(PlayerIndex.Four)) return true;
            return false;
        }

        public static PlayerIndex GetNextXBOXGamepad()
        {
            if (IsAnyButtonPressed(PlayerIndex.One)) return PlayerIndex.One;
            if (IsAnyButtonPressed(PlayerIndex.Two)) return PlayerIndex.Two;
            if (IsAnyButtonPressed(PlayerIndex.Three)) return PlayerIndex.Three;
            if (IsAnyButtonPressed(PlayerIndex.Four)) return PlayerIndex.Four;
            return PlayerIndex.One;
        }
    }
}